package com.hhsjdesign.test.act;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;

import com.google.android.material.snackbar.Snackbar;
import com.hhsj.design.pullableview.ability.IPullAble;
import com.hhsj.design.pullableview.ability.IPullUpAble;
import com.hhsj.design.pullableview.listview.RefreshListViewPro;
import com.hhsj.design.pullableview.view.ILoadMoreView;
import com.hhsjdesign.headerpullrefresh.R;
import com.hhsjtest.scaffold.ListScaffoldView;

import java.util.ArrayList;
import java.util.List;

public class ListViewActivity extends AppCompatActivity {
    private RefreshListViewPro listView;
    private MAdapter mAdapter;
    private TextView paramsView;
    private int i = 1;

    private final Handler mHandler = new Handler(Looper.getMainLooper());

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setTitle("ListView测试");

        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.setHomeButtonEnabled(true);
            actionBar.setDisplayHomeAsUpEnabled(true);
        }

        setContentView(R.layout.act_refresh_list);
        ListScaffoldView scaffold = findViewById(R.id.scaffold);


        TextView topNavigation = new TextView(this);
        topNavigation.setHeight(168);
        topNavigation.setText("顶部导航");
        topNavigation.setGravity(Gravity.CENTER_VERTICAL);
        topNavigation.setPadding(30, 0, 0, 0);
        scaffold.addTopNavigationView(topNavigation);

        scaffold.setFloatBarAction(ListScaffoldView.SCROLL_TO_TOP);
        ImageView floatImage = new ImageView(this);
        floatImage.setImageResource(R.drawable.progressbar_cycle);
        scaffold.addFloatBarView(floatImage);

        scaffold.setFloatBarViewMargin(50, 50);

        listView = findViewById(R.id.scaffold_scrollview);

        paramsView = findViewById(R.id.params);
        paramsView.setText(getText());


        TextView header = new TextView(this);
        header.setHeight(300);
        header.setBackgroundColor(0xFFFF33FF);
        header.setText("header1");
        header.setGravity(Gravity.CENTER);
        listView.addHeaderView(header);
        listView.setLoadMoreViewBgColor(0xFF880000);

//        listView.setLoadMoreView(null,300);

        listView.setLoadMoreView(new ILoadMoreView(this, new TextView(this)) {
            @Override
            public void createView(View view) {
                ((TextView) getLoadMoreView()).setGravity(Gravity.CENTER);
                view.setBackgroundColor(0xFF008800);
            }

            @Override
            public ViewGroup.MarginLayoutParams createLayoutParams() {
                ViewGroup.MarginLayoutParams params = new ViewGroup.MarginLayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                params.height = 200;
                return params;
            }

            @Override
            public int getLoadMoreHeight() {
                return 300;
            }

            @Override
            public void startAnim() {
                ((TextView) getLoadMoreView()).setText("Loading");
            }

            @Override
            public void stopAnim() {
                ((TextView) getLoadMoreView()).setText("Load End");
            }

            @Override
            public void updateText(@NonNull String... text) {
                ((TextView) getLoadMoreView()).setText(text[0]);
            }

            @Override
            public void updateWithState(IPullUpAble.FooterState state) {
                ((TextView) getLoadMoreView()).setText(state.toString());
            }

            @Override
            public int getBottomMargin() {
                return 20;
            }


        }, 300);

        TextView footer = new TextView(this);
        footer.setHeight(200);
        footer.setText("footer");
        footer.setBackgroundColor(0xFF33FFFF);
        footer.setGravity(Gravity.CENTER);
        listView.addFooterView(footer);

        listView.setPullAbleListener(new IPullAble.PullAbleListener() {
            @Override
            public void doRefresh() {
                mHandler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        listView.stopRefresh();
                    }
                }, 2_000);
            }

            @Override
            public boolean onContinuePullAction() {
                Snackbar.make(listView, "触发继续下拉动作", Snackbar.LENGTH_LONG).show();
                return true;
            }

            @Override
            public void doLoadMore() {
                mHandler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        i++;
                        mAdapter.setSize(i * 50);
                        if (i >= 6) {
                            listView.setLoadMoreEnable(false);
                        } else {
                            listView.stopLoadMore();
                        }
                    }
                }, 2_000);
            }

        });
        mAdapter = new MAdapter(ListViewActivity.this, 50);
        listView.setAdapter(mAdapter);

    }

    static class MAdapter extends BaseAdapter {

        private int size = 100;

        private Context context;

        public MAdapter(Context context, int size) {
            this.context = context;
            this.size = size;
        }

        public void setSize(int size) {
            this.size = size;
            notifyDataSetChanged();
        }

        @Override
        public int getCount() {
            return size;
        }

        @Override
        public Object getItem(int position) {
            return position;
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            TextView textView = new TextView(context);
            textView.setMinHeight(100);
            textView.setBackgroundColor(position % 2 == 0 ? 0x33FFFFFF : 0x33FF3030);
            textView.setGravity(Gravity.CENTER);
            textView.setText((position + 1) + "");
            return textView;
        }
    }

    private void testContinue() {
        List<String> numbers = new ArrayList<>();
        final String ZERO = "0";
        int i = 0;
        while (i < 600_000) {
            numbers.add(i + "");
            i++;
        }
        for (i = 0; i < numbers.size(); ) {
            if (numbers.get(i).contains(ZERO)) {
                numbers.remove(i);
                continue;
            }
            i++;
        }
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                this.finish(); // back button
                return true;
            case R.id.close_elastic:
                listView.setElasticEnable(false);
                break;

            case R.id.open_elastic:
                listView.setElasticEnable(true);
                break;

            case R.id.close_refresh:
                listView.setRefreshEnable(false);
                break;

            case R.id.open_refresh:
                listView.setRefreshEnable(true);
                break;

            case R.id.elaix_top:
                listView.setRefreshViewMode(IPullAble.REFRESH_VIEW_MODE_TOP);
                break;

            case R.id.elaix_mid:
                listView.setRefreshViewMode(IPullAble.REFRESH_VIEW_MODE_MID);
                break;

            case R.id.close_load_more:
                listView.setLoadMoreEnable(false);
                break;

            case R.id.open_load_more:
                listView.setLoadMoreEnable(true);
                break;

            case R.id.close_footer_line:
                listView.setFooterLineEnable(false);
                break;

            case R.id.open_footer_line:
                listView.setFooterLineEnable(true);
                break;

            case R.id.close_continue_pull:
                listView.setContinuePullEnable(false);
                break;

            case R.id.open_continue_pull:
                listView.setContinuePullEnable(true);
                break;

            case R.id.footerline_hide:
                listView.showFooterLine(false);
                break;

            case R.id.footerline_show:
                listView.showFooterLine(true);
                break;

        }
        paramsView.setText(getText());
        return super.onOptionsItemSelected(item);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    private String getText() {
        StringBuilder builder = new StringBuilder();

        builder.append("弹性功能：").append(listView.isElasticEnable())
                .append("\n").append("弹性位置：").append(listView.getRefreshViewMode())
                .append("\n").append("下拉刷新：").append(listView.isRefreshEnable())
                .append("\n").append("继续下拉：").append(listView.isContinuePullEnable())
                .append("\n").append("上拉加载：").append(listView.isLoadMoreEnable())
                .append("\n").append("底线功能：").append(listView.isFooterLineEnable())
                .append("\n").append("底线展示：").append(listView.isFooterLineShowing())
        ;

        return builder.toString();
    }
}
